<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Anonymous classes</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Defining-new-classes.xhtml" title="Defining new classes"/>
    <link rel="next" href="Enumerations.xhtml" title="Enumeration types"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Anonymous classes" epub:type="subchapter" id="Anonymous-classes">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Anonymous classes</h2>
          </div>
        </div>
      </div>
      <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871704240" class="indexterm"/> <code class="function">object</code> <em class="replaceable"><code><span class="bold"><strong>(</strong></span></code></em><em class="replaceable"><code>supers</code></em> <em class="replaceable"><code>...</code></em><em class="replaceable"><code><span class="bold"><strong>)</strong></span></code></em> <em class="replaceable"><code>field-or-method-decl</code></em> <em class="replaceable"><code>...</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Returns a new instance of an anonymous (inner) class.
The syntax is similar to <code class="literal">define-class</code>.
</p>
          <div class="literallayout">
            <p><a id="idm139667871698016" class="indexterm"/><span id="meta-object-field-or-method-decl"/><em class="replaceable"><code>object-field-or-method-decl</code></em> <code class="literal">::=</code> <a class="link" href="Anonymous-classes.xhtml#meta-object-field-decl"><em class="replaceable"><code>object-field-decl</code></em></a> | <a class="link" href="Defining-new-classes.xhtml#meta-method-decl"><em class="replaceable"><code>method-decl</code></em></a><br/>
<a id="idm139667871694352" class="indexterm"/><span id="meta-object-field-decl"/><em class="replaceable"><code>object-field-decl</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code><a class="link" href="Defining-new-classes.xhtml#meta-field-name"><em class="replaceable"><code>field-name</code></em></a> (<a class="link" href="Annotations.xhtml#meta-annotation"><em class="replaceable"><code>annotation</code></em></a> | <a class="link" href="Types.xhtml#meta-opt-type-specifier"><em class="replaceable"><code>opt-type-specifier</code></em></a> | <a class="link" href="Defining-new-classes.xhtml#meta-field-option"><em class="replaceable"><code>field-option</code></em></a>)*  [<a class="link" href="Anonymous-classes.xhtml#meta-object-init"><em class="replaceable"><code>object-init</code></em></a>] <code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
<a id="idm139667871686816" class="indexterm"/><span id="meta-object-init"/><em class="replaceable"><code>object-init</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
</p>
          </div>
          <p>Returns a new instance of a unique (anonymous) class.
The class inherits from the list of <em class="replaceable"><code>supers</code></em>, where at most one of the
elements should be the base class being extended from, and the rest
are interfaces.
</p>
          <p>This is roughly equivalent to:
</p>
          <pre class="screen">(begin
  (define-simple-class <em class="replaceable"><code>hname</code></em> (<em class="replaceable"><code>supers</code></em> ...) <em class="replaceable"><code>field-or-method-decl</code></em> ...)
  (make <em class="replaceable"><code>hname</code></em>))
</pre>
          <p>A <em class="replaceable"><code>field-decl</code></em> is as for <code class="literal">define-class</code>, except
that we also allow an abbreviated syntax.
Each <em class="replaceable"><code>field-decl</code></em> declares a public instance field.
If <em class="replaceable"><code>object-finit</code></em> is given, it is an expression whose value
becomes the initial value of the field.
The <em class="replaceable"><code>object-init</code></em> is evaluated at the same time as the <code class="literal">object</code>
expression is evaluated,
in a scope where all the <em class="replaceable"><code>field-name</code></em>s are visible.
</p>
          <p>A <em class="replaceable"><code>method-decl</code></em> is as for <code class="literal">define-class</code>.
</p>
        </blockquote>
      </div>
      <span id="SAM-conversion"/>
      <section class="sect2" title="Lambda as shorthand for anonymous class" epub:type="division" id="idm139667871675040">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Lambda as shorthand for anonymous class</h3>
            </div>
          </div>
        </div>
        <p>An anonymous class is commonly used in the Java platform where a
function language would use a lambda expression.
Examples are call-back handlers, events handlers, and <code class="literal">run</code> methods.
In these cases Kawa lets you use a lambda expression as a short-hand
for an anonymous class.  For example:
</p>
        <pre class="screen">(button:addActionListener
  (lambda (e) (do-something)))
</pre>
        <p>is equivalent to:
</p>
        <pre class="screen">(button:addActionListener
  (object (java.awt.event.ActionListener)
    ((actionPerformed (e ::java.awt.event.ActionEvent))::void
     (do-something))))
</pre>
        <p>This is possible when the required type is an interface or
abstract class with a Single (exactly one) Abstract Methods.
Such a class is sometypes called a <em class="firstterm">SAM-type</em>, and the
conversion from a lambda expression to an anonymous class
is sometimes called <em class="firstterm">SAM-conversion</em>.
</p>
        <p>Note that Kawa can also infer the parameter and return types
of a method that overrides a method in a super-class.
</p>
      </section>
    </section>
    <footer>
      <div class="navfooter">
        <ul>
          <li>
            <b class="toc">
              <a href="Anonymous-classes.xhtml#idm139667871675040">Lambda as shorthand for anonymous class</a>
            </b>
          </li>
        </ul>
        <p>
          Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
        <p>
        Previous: <a accesskey="p" href="Defining-new-classes.xhtml">Defining new classes</a></p>
        <p>
        Next: <a accesskey="n" href="Enumerations.xhtml">Enumeration types</a></p>
      </div>
    </footer>
  </body>
</html>
